package com.smbms.controller;

import com.smbms.pojo.User;
import com.smbms.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
@Controller
public class LoginController {

    @Resource
    private UserService userService;

    @RequestMapping(value = "/login.html")
    public String login(){
        return "login";
    }

    @RequestMapping(value = "/dologin.html",method = RequestMethod.POST)
    public String doLogin(@RequestParam String userCode,
                          @RequestParam String userPassword,
                          HttpSession session,
                          HttpServletRequest request){
        //调用service方法，进行用户匹配
        User user = userService.login(userCode,userPassword);
        if (null != user){//登陆成功
            //页面跳转(frame.jsp)
            //session.setAttribute(Constants.USER_SESSION,user);
            session.setAttribute("userSession",user);
            return "redirect:/main.html";
            //response.sendRedirect("jsp/frame.jsp")
        }else {
            //页面跳转(login.jsp)带出提示信息——转发
            request.setAttribute("error","用户名或密码不正确");
            return "login";
        }
    }

    @RequestMapping(value = "/main.html")
    public String main(){
        return "frame";
    }

    @RequestMapping(value = "/logout")
    public String logout(HttpSession session){
        //session.removeAttribute(Constants.USER_SESSION);
        return "login";
    }
}
